<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Statistics' );
?>
					<div class="blok">
						<strong><?php transprint( 'Statistics' ); ?></strong><br />
<?php
$max_villages = getVillageLimit();
$query2 = "SELECT count(*) FROM village WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$number_of_villages = $row2[ 'count(*)' ];
print( translate ( "Villages" ) . ": " . $number_of_villages . " " . translate( 'out of' ) . " $max_villages<br /><br />\r\n" );

if( $number_of_villages > 0 ) {
	$query2 = "SELECT MAX(food_quality) " .
			"FROM village " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$highest = $row2[ 'MAX(food_quality)' ];
	$query2 = "SELECT SUM(food_quality) FROM village WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	print( translate ( "Average food quality" ) . ": <strong class=\"food\">" . round( $row2[ 'SUM(food_quality)' ] / $number_of_villages, 2 ) . "%</strong> " . translate( 'your highest' ) . ": <strong class=\"food\">$highest%</strong><br />\r\n" );
	$query2 = "SELECT MAX(coal_quality) " .
			"FROM village " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$highest = $row2[ 'MAX(coal_quality)' ];
	$query2 = "SELECT SUM(coal_quality) FROM village WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	print( translate ( "Average coal quality" ) . ": <strong class=\"coal\">" . round( $row2[ 'SUM(coal_quality)' ] / $number_of_villages, 2 ) . "%</strong> " . translate( 'your highest' ) . ": <strong class=\"coal\">$highest%</strong><br />\r\n" );
	$query2 = "SELECT MAX(iron_quality) " .
			"FROM village " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$highest = $row2[ 'MAX(iron_quality)' ];
	$query2 = "SELECT SUM(iron_quality) FROM village WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	print( translate ( "Average iron quality" ) . ": <strong class=\"iron\">" . round( $row2[ 'SUM(iron_quality)' ] / $number_of_villages, 2 ) . "%</strong> " . translate( 'your highest' ) . ": <strong class=\"iron\">$highest%</strong><br /><br />\r\n" );
}
print( translate( 'Season' ) . ': ' . translate( getSeasonString() ) . '<br />' );
$query2 = "SELECT SUM(population) FROM village WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$population = $row2[ 'SUM(population)' ];
$query2 = "SELECT SUM(scout+infantry+cavalry+artillery) FROM army WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$population += $row2[ 'SUM(scout+infantry+cavalry+artillery)' ];
$query2 = "SELECT recruits FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$population += $row2[ 'recruits' ];
$query2 = "SELECT SUM(population) FROM colony WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$population += $row2[ 'SUM(population)' ];
print( translate ( "Total population" ) . ": " . number_format( $population ) . "<br />\r\n" );
$query2 = "SELECT SUM(population) FROM village";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$query3 = "SELECT SUM(scout+infantry+cavalry+artillery) FROM army";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$world_pop =  $row2[ 'SUM(population)' ] + $row3[ 'SUM(scout+infantry+cavalry+artillery)' ];
$query3 = "SELECT SUM(recruits) FROM ruler";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$world_pop += $row3[ 'SUM(recruits)' ];
$query3 = "SELECT SUM(population) FROM colony";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$world_pop += $row3[ 'SUM(population)' ];
print( translate ( "World population" ) . ": " . number_format( $world_pop ) . "<br />\r\n" );
print( translate ( "Your percent of world population" ) . ": " . round( $population / $world_pop * 100, 2 ) . "%<br />\r\n" );
print( "</div>\r\n<br />\r\n" );

print( "<table id=\"overview\">\r\n" );
$query = "SELECT * " .
		"FROM ruler " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( $row[ 'scouts_killed' ] > 0 || $row[ 'scouts_lost' ] > 0 ) {
	print( "<tr><td>" . translate ( "Scouts killed" ) . "</td><td align=\"right\">" . number_format( $row[ 'scouts_killed' ] ) . "</td><td class=\"middle\"></td>\r\n");
	print( "<td>" . translate ( "Scouts lost" ) . "</td><td align=\"right\">" . number_format( $row[ 'scouts_lost' ] ) . "</td></tr>\r\n");
}

if( $row[ 'scouts_killed' ] > 0 && $row[ 'scouts_lost' ] > 0 ) {
	print( "<tr><td>" . translate ( "Scout kill/loss ratio" ) . "</td><td align=\"right\">" . round( $row[ 'scouts_killed' ] / $row[ 'scouts_lost' ], 2 ) . "</td><td class=\"middle\"></td><td></td><td></td></tr>\r\n" );
}

if( $row[ 'infantry_killed' ] > 0 || $row[ 'infantry_lost' ] > 0 ) {
	print( "<tr><td>" . translate ( "Infantry killed" ) . "</td><td align=\"right\">" . number_format( $row[ 'infantry_killed' ] ) . "</td><td class=\"middle\"></td>\r\n");
	print( "<td>" . translate ( "Infantry lost" ) . "</td><td align=\"right\">" . number_format( $row[ 'infantry_lost' ] ) . "</td></tr>\r\n");
}

if( $row[ 'infantry_killed' ] > 0 && $row[ 'infantry_lost' ] > 0 ) {
	print( "<tr><td>" . translate ( "Infantry kill/loss ratio" ) . "</td><td align=\"right\">" . round( $row[ 'infantry_killed' ] / $row[ 'infantry_lost' ], 2 ) . "</td><td class=\"middle\"></td><td></td><td></td></tr>\r\n" );
}

if( $row[ 'cavalry_killed' ] > 0 || $row[ 'cavalry_lost' ] > 0 ) {
	print( "<tr><td>" . translate ( "Cavalry killed" ) . "</td><td align=\"right\">" . number_format( $row[ 'cavalry_killed' ] ) . "</td><td class=\"middle\"></td>\r\n");
	print( "<td>" . translate ( "Cavalry lost" ) . "</td><td align=\"right\">" . number_format( $row[ 'cavalry_lost' ] ) . "</td></tr>\r\n");
}

if( $row[ 'cavalry_killed' ] > 0 && $row[ 'cavalry_lost' ] > 0 ) {
	print( "<tr><td>" . translate ( "Cavalry kill/loss ratio" ) . "</td><td align=\"right\">" . round( $row[ 'cavalry_killed' ] / $row[ 'cavalry_lost' ], 2 ) . "</td><td class=\"middle\"></td><td></td><td></td></tr>\r\n" );
}

if( $row[ 'artillery_killed' ] > 0 || $row[ 'artillery_lost' ] > 0 ) {
	print( "<tr><td>" . translate ( "Artillery killed" ) . "</td><td align=\"right\">" . number_format( $row[ 'artillery_killed' ] ) . "</td><td class=\"middle\"></td>\r\n");
	print( "<td>" . translate ( "Artillery lost" ) . "</td><td align=\"right\">" . number_format( $row[ 'artillery_lost' ] ) . "</td></tr>\r\n");
}
if( $row[ 'artillery_killed' ] > 0 && $row[ 'artillery_lost' ] > 0 ) {
	print( "<tr><td>" . translate ( "Artillery kill/loss ratio" ) . "</td><td align=\"right\">" . round( $row[ 'artillery_killed' ] / $row[ 'artillery_lost' ], 2 ) . "</td><td class=\"middle\"></td><td></td><td></td></tr>\r\n" );
}
$score_killed = $row[ 'scouts_killed' ] * 32 + $row[ 'infantry_killed' ] * 48 + $row[ 'cavalry_killed' ] * 256 + $row[ 'artillery_killed' ] * 400;
$score_lost = $row[ 'scouts_lost' ] * 32 + $row[ 'infantry_lost' ] * 48 + $row[ 'cavalry_lost' ] * 256 + $row[ 'artillery_lost' ] * 400;
if( $score_killed > 0 ) {
	print( "<tr><td>" . translate ( "Score killed" ) . "</td><td align=\"right\">" . number_format( $score_killed ) . "</td><td class=\"middle\"></td>\r\n");
	print( "<td>" . translate ( "Score lost" ) . "</td><td align=\"right\">" . number_format( $score_lost ) . "</td></tr>\r\n");
}
if( $score_killed > 0 && $score_lost > 0 ) {
	print( "<tr><td>" . translate ( "Score kill/loss ratio" ) . "</td><td align=\"right\">" . round( $score_killed / $score_lost, 2 ) . "</td><td class=\"middle\"></td><td></td><td></td></tr>\r\n" );
}
print( "</table>\r\n" );
print( "<br />\r\n" );
$query = "SELECT count(*) " .
		"FROM village " .
		"WHERE start_place = 1";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$total_spaces = $row[ 'count(*)' ];

$query = "SELECT count(*) FROM ruler";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$total_rulers = $row[ 'count(*)' ];

$query = "SELECT speed, max_turn, turn, server_message FROM game";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$speed = $row[ 'speed' ];
$speedtext = ( int ) ( $speed / 60 ) . ":";
$speed = ( $speed % 60 );
if( $speed < 10 ) {
	$speed = "0" . $speed;
}
$speedtext = $speedtext . $speed;
print( "<div class=\"blok\">\r\n" );
print( "<strong>" . translate ( "Game information" ) . "</strong><br/>\r\n" );
print( "" . translate ( "Turn speed (minutes:seconds)" ) . ": " . $speedtext . "<br/>\r\n" );
if( $row[ 'turn' ] > 1 && $row[ 'turn' ] != $row[ 'max_turn' ] ) {
	$countdown = $row[ 'speed' ] - time() % $row[ 'speed' ];
	$speedtext = ( int ) ( $countdown / 60 ) . ":";
	$countdown = ( $countdown % 60 );
	if( $countdown < 10 ) {
		$countdown = "0" . $countdown;
	}
	$speedtext = $speedtext . $countdown; 						
	print( "\t\t\t\t\t" . translate ( "The next turn update will be in" ) . ": $speedtext<br />\r\n" );
}
print( translate ( "Days passed" ) . ": " . number_format( $row[ 'turn' ] ) . "<br/>\r\n" );
print( translate ( "End day" ) . ": " . number_format( $row[ 'max_turn' ] ) . "<br/>\r\n" );

$query2 = "SELECT * FROM map_settings";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC);
print( "<br />\r\n" . translate ( "Current sector" ) . ": " . $row2[ 'sector' ] . "<br />\r\n" );
print( translate ( "Sector range" ) . ": x: " . $row2[ 'x' ] . "-" . ( $row2[ 'x' ] + $row2[ 'width' ] ) . ", y: " . $row2[ 'y' ] . "-" . ( $row2[ 'y' ] + $row2[ 'height' ] ) . "<br />\r\n" );
$query3 = "SELECT count(*) FROM village WHERE start_place = 1 AND ruler_id = -1";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC);
print( translate ( "Capitals available" ) . ": " . number_format( $row3[ 'count(*)' ] ) . " " . translate( 'out of' ) . " " . number_format( $row2[ 'starting_places' ] ) . " " . translate( 'capitals in this sector' ) . "<br />\r\n" );
$query3 = "SELECT count(*) FROM village WHERE start_place = 1 AND ruler_id != -1";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC);
print( translate ( "Rulers" ) . ": " . number_format( $row3[ 'count(*)' ] ) . " " . translate( 'out of' ) . " " . number_format( $total_spaces ) . "<br/>\r\n" );
$query2 = "SELECT count(*) FROM village";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC);
$total_villages = $row2[ 'count(*)' ];
$query2 = "SELECT count(*) FROM village WHERE ruler_id = -1";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC);
$not_taken_villages = $row2[ 'count(*)' ];
print( "<br />\r\n" . translate ( "Total villages" ) . ": " . number_format( $total_villages ) . "<br />\r\n" );
print( translate ( "Villages owned by rulers" ) . ": " . number_format( $total_villages - $not_taken_villages ) .  "<br />\r\n" );
print( translate ( "Villages owned by independents" ) . ": " . number_format( $not_taken_villages ) . "<br />\r\n" );
print( "<br />" );
print( translate ( "Controlled village average" ) . ": " . round( ( $total_villages - $not_taken_villages ) / $total_rulers, 2 ) . "<br />\r\n" );

$query3 = "SELECT SUM(last_year_score) FROM ruler WHERE score > 0 AND last_year_score > 0";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$query2 = "SELECT SUM(score) FROM ruler WHERE score > 0 AND last_year_score > 0";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );

if( $row2[ 'SUM(score)' ] == 0 || $row3[ 'SUM(last_year_score)' ] == 0 ) {
	$growth = '-';
}
else {
	$growth = round( ( $row2[ 'SUM(score)' ] / $row3[ 'SUM(last_year_score)' ] - 1.0 ) * 100 , 2 ) . '%';
	//print( "sum(score): " . $row2[ 'SUM(score)' ] . " / " . $row3[ 'SUM(last_year_score)' ] . " = " . ( $row2[ 'SUM(score)' ] / $row3[ 'SUM(last_year_score)' ] ) . "<br />" );
}

$query3 = "SELECT SUM(last_year_growth) FROM ruler";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
if( $row3[ 'SUM(last_year_growth)' ] == 0 ) {
	$last_year_growth = '-';
}
else {
	$last_year_growth = round( $row3[ 'SUM(last_year_growth)' ] / $total_rulers, 2 ) . '%';
}

print( translate ( "Average score growth" ) . ": $growth<br />\r\n" );
print( translate ( "Last years average score growth" ) . ": $last_year_growth<br />\r\n" );

$query3 = "SELECT count(*) " .
		"FROM translations";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$total_texts = $row3[ 'count(*)' ];

$query3 = "SELECT count(*) " .
		"FROM translations " .
		"WHERE translated = 1";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$total_translated = $row3[ 'count(*)' ];

$query3 = "SELECT count(*) " .
		"FROM translations " .
		"WHERE approved = 1";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$total_approved = $row3[ 'count(*)' ];

$query3 = "SELECT count(*) " .
		"FROM languages";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$total_languages = $row3[ 'count(*)' ];

print( '<br />' );
print( translate( 'Number of available languages' ) . ': ' . number_format( $total_languages ) . '<br />' );
print( translate( 'Total number of translatable texts' ) . ': ' . number_format( $total_texts ) . '<br />' );
print( translate( 'Translated texts' ) . ': ' . number_format( $total_translated ) . '<br />' );
print( translate( 'Translated and approved texts' ) . ': ' . number_format( $total_approved ) . '<br />' );

print( "<br />" . translate ( "Server message" ) . "<br/>\r\n<pre>" . html_entity_decode( stripslashes( $row[ 'server_message' ] ) ) . "</pre>\r\n" );
?>
			</div>
<?php
aos_footer();
?>